package com.han.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SubcriptionSubject
 * @Description 具体主题角色类
 * @Author duoduo Han
 * @Date 2024/6/17 10:41
 */
public class SubcriptionSubject implements Subject {

    //定义一个集合用来存储多个观察者对象
    private List<Observer> weixinUserList = new ArrayList<>();

    @Override
    public void attach(Observer observer) {
        weixinUserList.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        weixinUserList.remove(observer);
    }

    @Override
    public void notifyObserver(String msg) {
        //遍历集合
        for (Observer observer : weixinUserList) {
            //调用观察者对象中的update方法
            observer.update(msg);
        }
    }
}
